package com.manager.car.dao;


import java.util.List;

import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.springframework.orm.hibernate5.HibernateCallback;

import com.manager.car.po.ManagerInfo;
import com.manager.car.util.PageModel;



public class ManagerInfoDaoImpl extends BaseDao<ManagerInfo>  implements IManagerInfoDao{
	
	private PageModel<ManagerInfo> pageModel;
	
	//get a element from query id
	public ManagerInfo getElement(Integer id){
		
		return this.getHibernateTemplate().get(ManagerInfo.class, id);
	}
	
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public PageModel<ManagerInfo> queryPageModelByHql(final int pageNo ,final int pageSize,final String hql) {
		 this.pageModel = (PageModel<ManagerInfo>)this.getHibernateTemplate().execute(new HibernateCallback() {

			public PageModel<ManagerInfo> doInHibernate(Session session) throws HibernateException {
				List<ManagerInfo> list = session.createQuery(hql)
						.setFirstResult((pageNo-1)*pageSize)
						.setMaxResults(pageSize)
						.list();
				List<ManagerInfo> counts = session.createQuery(hql).list();
				int totalCount = counts.size();
				pageModel.setCode(0);
				pageModel.setCount(totalCount);
				pageModel.setMsg("我不是懒，我只是没学！");
				pageModel.setData(list);
				return pageModel;
			}
		});
		return pageModel;
	}

	//update ManagerInfoStatic equals 0
	public boolean updataManagerInfoStaticById(Integer id){
		ManagerInfo mi = queryById(id);
		boolean bool = false;
		if(mi != null) {
			mi.setManagerStatic(0);
			this.update(mi);
			bool = true;
		}
		return bool;
	}

	public PageModel<ManagerInfo> getPageModel() {
		return pageModel;
	}

	public void setPageModel(PageModel<ManagerInfo> pageModel) {
		this.pageModel = pageModel;
	}

	public List<ManagerInfo> getQueryListByHql(String hql) {
		return queryByHql(hql);
	}
	
	

}
